package Components;

import java.awt.Color;
import java.awt.Transparency;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Connector.Connector;
import Starter.BroadcastListener;
import Starter.Menu;
import smash_tris.*;

public class CreatorPanel extends JPanel {

	private Menu motherPanel;
	private ImageButton create;
	private String DIR = "picture/menu_pics/";
	private JTextField nameRoom;
	private GameWindow gameWindow = null;
	
	private BroadcastListener broadcastListener = null;
	//private Connector connector = null;

	public CreatorPanel(Menu panel) {
		motherPanel = panel;
		//this.connector = connector;
		
		this.setLayout(null);
		this.setVisible(true);
		this.setSize(250, 80);

		Color color = new Color(255, 255, 255, 100);
		System.out.println(color.getAlpha());
		this.setBackground(color);

		initComponents();
	}
	
	public void setBroadcastListener(BroadcastListener broadcastListener) {
		this.broadcastListener = broadcastListener;
	}
	
	public GameWindow getGameWindow() {
		return gameWindow;
	}

	private void initComponents() {

		nameRoom = new JTextField();

		final JTextField nameRoom = new JTextField();

		JLabel nazwa = new JLabel("Wpisz nazwe pokoju");

		create = new ImageButton(DIR + "create_UP.png", DIR + "create_DOWN.png");
		create.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				if (motherPanel.getConnector() != null) {
					if (nameRoom.getText().length() > 0) {
						Room r = motherPanel.getConnector().createRoom(
								nameRoom.getText());
						
						System.out.println("after create room");
						if (r != null) {
							//Room r = (Room) obj;
							System.out.println("creatorPanel:\t" +"pre\tGameWindow" );


							
							motherPanel.getMainFrame().setVisible(false);

							

							//motherPanel.getConnector().addPlayerToRoom(r);

							gameWindow = new GameWindow(
									motherPanel.getConnector(), r);
							
							broadcastListener.setGameWindow(gameWindow);
							
							//motherPanel.getRoomsPane().setRooms(actualRooms);
							//motherPanel.getRoomsPane().revalidate();
						}
					} else {
						System.out.println("Nazwa pokoju nie została podana.");
					}
					// TODO dodanie gracza do nowego pokoju
					// motherPanel.getConnector().createRoom(nameRoom.getText());
					/*
					 * motherPanel.setVisible(false);
					 * motherPanel.setEnabled(false);
					 */
					// String fin = nameRoom.getText();

					// Room room =
					// motherPanel.getConnector().createRoom(fin).get(0);
					// Room room = new Room(motherPanel.getPlayer(),fin);
					/*
					 * GameWindow GW = new
					 * GameWindow(motherPanel.getConnector(), room,
					 * motherPanel);
					 */
				} else {
					System.out.print("NULL");
				}
			}
		});

		
		
		nameRoom.setBounds(90, 40, 150, 20);
		nazwa.setBounds(90, 10, 150, 30);
		create.setBounds(10, 10, 62, 62);

		this.add(nameRoom);
		this.add(nazwa);
		this.add(create);

	}

}
